<?php

/*
 * Class to handle sponsors
 */

class sponsor {

  //Properties

  /*
   * Id of the sponsor in the database
   */

  public $id = null;
    
  /*
   * Name of the sponsor
   */
    
  public $name = null;

  /*
   * Image source
   */

  public $image = null;

  /*
   * Link to sponsors website
   */

  public $url = null;

  public function __construct( $data=array() ) {
    if ( isset( $data['id'] ) ) $this->id = (int)$data['id'];
    if ( isset( $data['name'] ) ) $this->name = $data['name'];
	  if ( isset( $data['image'] ) ) $this->image = $data['image'];
	  if ( isset( $data['url'] ) ) $this->url = $data['url'];
	}

	public function storeFormValues( $params ) {
	  $this->__construct( $params );
	}

	public static function getById( $id ) {
	  $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql = "SELECT * FROM sponsor WHERE id = :id";
      $st = $conn->prepare( $sql );
      $st->bindValue( ":id", $id, PDO::PARAM_INT );
      $st->execute();
      $row = $st->fetch();
      $conn = null;
      if ( $row ) return new Sponsor( $row );
	}

	public static function getList( $numRows=1000000) {
      $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM sponsor
              LIMIT :numRows";

      $st = $conn->prepare( $sql );
      $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
      $st->execute();
      $list = array();

      while ( $row = $st->fetch() ) {
        $sponsor = new sponsor( $row );
        $list[] = $sponsor;
      }

      // Now get the total number of home that matched the criteria
      $sql = "SELECT FOUND_ROWS() AS totalRows";
      $totalRows = $conn->query( $sql )->fetch();
      $conn = null;
      return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );

	}

	public function insert() {

	    // Does the home object already have an ID?
	    if ( !is_null( $this->id ) ) trigger_error ( "Sponsor::insert(): Attempt to insert an sponsor object that already has its ID property set (to $this->id).", E_USER_ERROR );

	    // Insert the home
	    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
	    $this->id = $conn->lastInsertId();
	    $sql = "INSERT INTO sponsor ( id, name, image, url ) VALUES ( :id, :name, :image, :url )";
	    $st = $conn->prepare ( $sql );
	    $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
      $st->bindValue( ":name", $this->name, PDO::PARAM_STR );
	    $st->bindValue( ":image", $this->image, PDO::PARAM_STR );
	    $st->bindValue( ":url", $this->url, PDO::PARAM_STR );
	    $st->execute();
	    $conn = null;
    }

    public function update() {
      if ( is_null( $this->id ) ) trigger_error ( "Sponsor::update(): Attempt to update an sponsor object that does not have its ID property set.", E_USER_ERROR );

      $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql = "UPDATE sponsor SET name=:name, image=:image, url=:url WHERE id = :id";
      $st = $conn->prepare ( $sql );
      $st->bindValue( ":name", $this->name, PDO::PARAM_STR );
      $st->bindValue( ":image", $this->image, PDO::PARAM_STR );
      $st->bindValue( ":url", $this->url, PDO::PARAM_STR );
      $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
      $st->execute();
      $conn = null;
    }

    public function delete() {
      if ( is_null( $this->id ) ) trigger_error ( "Sponsor::delete(): Attempt to delete an sponsor object that does not have an ID property.", E_USER_ERROR );

      if( unlink( $this->image ) ) {
        $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
        $sql = "DELETE FROM sponsor WHERE id=:id LIMIT 1";
        $st = $conn->prepare( $sql );
        $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
        $st->execute();
        $conn = null;
      } else {
        trigger_error( "Sponsor::delete() : Attempt to delete the image failed.", E_USER_ERROR);
      }
    }
  }

?>
